// pages/partnercenter/mynews/mynews.js  partner_inform
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
var app = getApp();
var range
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    newslistflag: true,
    mask_flag: false,
    showLoading: true,
    all_news_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.refreshnewslist()
    range = 1200 / winScale;
    var animation = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
  },
  refreshnewslist: function() {
    var that = this
    shihan.sendRequest(config.partner_inform, 'POST', {
        page: this.data.page,
        page_size: this.data.page_size
      })
      .then(function(response) {
        if (response.data.code == 0) {
          if (that.data.page == 1) {
            var messageids = wx.getStorageSync('messageids') || '';
            //{"sh_activity_id":230,"sh_partner_inform_id":1,"sh_customer_salesrule_id":0}
            if (response.data.data.sh_items.length > 0) {
              wx.setStorage({
                key: 'messageids',
                data: {
                  "sh_activity_id": messageids.sh_activity_id,
                  "sh_partner_inform_id": response.data.data.sh_items[0].sh_id,
                  "sh_customer_salesrule_id": messageids.sh_customer_salesrule_id
                }
              })
            } else {
              wx.setStorage({
                key: 'messageids',
                data: {
                  "sh_activity_id": messageids.sh_activity_id,
                  "sh_partner_inform_id": 0,
                  "sh_customer_salesrule_id": messageids.sh_customer_salesrule_id
                }
              })
            }
          }
          that.setData({
            page: that.data.page + 1,
            all_news_list: that.data.all_news_list.concat(response.data.data.sh_items),
            newslistflag: true,
            showLoading: false
          })
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            newslistflag: false,
            showLoading: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  lookInfo: function(e) { //查看消息详情
    this.newsdetail(e.currentTarget.dataset.id)
    this.showPopup(1)
  },
  closeInfo: function() {
    this.showPopup(0)
  },
  showPopup: function (off) {
    range = ~range;
    this.animation.translateY(range).step();
    this.setData({
      mask_flag: !this.data.mask_flag,
      animationData: this.animation.export()
    })
  },
  newsdetail: function(id) {
    var that = this
    shihan.sendRequest(config.partner_inform_view, 'POST', {
        id: id
      })
      .then(function(response) {
        if (response.data.code == 0) {
          that.setData({
            newsdetail: response.data.data
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      all_news_list: [],
      showLoading: true,
    });
    this.refreshnewslist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.newslistflag) {
      that.setData({
        newslistflag: false
      })
      this.refreshnewslist()
    }
  }
})